/*
 * Copyright 2013 Walter Lütgenau.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package offset.nodes.client.chooser.view;

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import offset.nodes.client.view.NodesLookAndFeelManager;

/**
 * A textarea for a tree cell renderer. Unlike a DefaultTreeCellRenderer, the text area supports
 * line wrap.
 *
 * @author Walter Lütgenau
 */
public class TreeCellTextArea extends JTextArea {

    public static final int MAX_COLUMNS = 15;
    Dimension preferredSize;
    DefaultHighlighter highlighter = new DefaultHighlighter();
    Color foreground;
    Color background;
    Color selectionForeground;
    Color selectionBackground;
    DefaultHighlighter.DefaultHighlightPainter painter;

    /**
     * Constructor
     */
    public TreeCellTextArea() {
        setFont(UIManager.getFont("Tree.font"));
        setLineWrap(true);
        setWrapStyleWord(true);
        setColumns(MAX_COLUMNS);
        setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        // store color values (see below)
        foreground = UIManager.getColor("Tree.textForeground");
        setForeground(foreground);
        selectionForeground = UIManager.getColor("Tree.selectionForeground");
        selectionBackground = UIManager.getColor("Tree.selectionBackground");
        background = UIManager.getColor("Tree.textBackground");
        painter = new DefaultHighlighter.DefaultHighlightPainter(selectionBackground);
        setBackground(background);

        // highlight selected text using the highlighter
        setHighlighter(highlighter);
    }

    public void setBackground(Color color) {
        if (color instanceof ColorUIResource) {
            color = null;
        }
        super.setBackground(color);
    }

    /**
     * Draw text based on the selection state. If selected, use a DefaultHighlightPainter to draw
     * the background.
     *
     * @param isSelected is the cell selected or not?
     */
    public void setSelect(boolean isSelected) {
        if (isSelected) {
            setForeground(selectionForeground);
            try {
                highlighter.addHighlight(0, getText().length(), painter);
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
        } else {
            setBackground(background); // TODO: remove
            setForeground(foreground);
            highlighter.removeAllHighlights();
        }
    }
}
